//
//  MainTabBarController.swift
//  TJJPT
//
//  Created by FENGYL on 2017/6/1.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
        
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setUpTabBarItem()
        
        let timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(autoLogin), userInfo: nil, repeats: true)
        
        timer.fire()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - 设置tabbarItem
    func setUpTabBarItem() {
        
        for item in self.tabBar.items! {
            
            if item.tag == 7000 {
                
                item.title = NSLocalizedString(LOCALIZABLE_STRING_HOME, comment: "")
                
                
            }else if item.tag == 7001{
                
                item.title = NSLocalizedString(LOCALIZABLE_STRING_DEVICE_SELECTION, comment: "")
                
            }else if item.tag == 7002{
                
                item.title = NSLocalizedString(LOCALIZABLE_STRING_NOTIFICATION_CENTER, comment: "")
                
            }
            
        }
        
    }
    
    // MARK: - 自动过期登录
    func autoLogin() {
        
        if (UserDefaults.standard.value(forKey: USER_DEFAULTS_IS_LOGON) != nil) {
            
            var loading = false
            
            let isLogin:Bool = UserDefaults.standard.value(forKey: USER_DEFAULTS_IS_LOGON) as! Bool
            
            if isLogin == true && !loading{
                
                if (UserModel.userModel.lastConectedDate == nil) {
                    
                    UserModel.userModel.lastConectedDate = Date.init()
                    
                }
                
                let nowDate = Date.init()
                
                let result = nowDate.timeIntervalSince(UserModel.userModel.lastConectedDate!)
                
                if result > 600 {
                    
                    let userName = UserDefaults.standard.value(forKey: USER_DEFAULTS_USER_NAME)
                    
                    let password = UserDefaults.standard.value(forKey: USER_DEFAULTS_PASSWORD)
                    
                    if userName == nil || password == nil{
                        
                        return
                        
                    }
                    
                    loading = true
                    
                    NetworkCommand.login(userName: userName as! String, password: password as! String, success: { (response) in
                        
                        loading = false
                        
                        UserModel.userModel.lastConectedDate = Date.init()
                        
                        print("auto login success")
                        
                    }, failure: { (Error) in
                        
                        loading = false
                        
                    })
                    
                }
                
            }
            
        }
        
    }
    
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
